﻿/*
Last update: 16.05.2010, lembit@ux.ee
- 16.05.2010, lembit@ux.ee: added userProfileComplete()
- 16.05.2010, lembit@ux.ee: updated CSS Browser Selector, v0.3.3 => v0.3.5
*/

/*
CSS Browser Selector v0.3.5 (Feb 05, 2010)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u) { var ua = u.toLowerCase(), is = function (t) { return ua.indexOf(t) > -1; }, g = 'gecko', w = 'webkit', s = 'safari', o = 'opera', h = document.documentElement, b = [(!(/opera|webtv/i.test(ua)) && /msie\s(\d)/.test(ua)) ? ('ie ie' + RegExp.$1) : is('firefox/2') ? g + ' ff2' : is('firefox/3.5') ? g + ' ff3 ff3_5' : is('firefox/3') ? g + ' ff3' : is('gecko/') ? g : is('opera') ? o + (/version\/(\d+)/.test(ua) ? ' ' + o + RegExp.$1 : (/opera(\s|\/)(\d+)/.test(ua) ? ' ' + o + RegExp.$2 : '')) : is('konqueror') ? 'konqueror' : is('chrome') ? w + ' chrome' : is('iron') ? w + ' iron' : is('applewebkit/') ? w + ' ' + s + (/version\/(\d+)/.test(ua) ? ' ' + s + RegExp.$1 : '') : is('mozilla/') ? g : '', is('j2me') ? 'mobile' : is('iphone') ? 'iphone' : is('ipod') ? 'ipod' : is('mac') ? 'mac' : is('darwin') ? 'mac' : is('webtv') ? 'webtv' : is('win') ? 'win' : is('freebsd') ? 'freebsd' : (is('x11') || is('linux')) ? 'linux' : '', 'js']; c = b.join(' '); h.className += ' ' + c; return c; }; css_browser_selector(navigator.userAgent);

/* init jQuery-based functions */
$(document).ready(function () {
	ieHover('ul.subnav > li, #menu > li, .user-profile > div, #user-menu > div, #user-menu2 > div');
	initSlide();
	initClose();
	initGall();
	initAccordion();
	clearInputs();
	eduToggle();
	userProfileComplete();
});
/* user profile completion msg */
function userProfileComplete() {
	if ($('.user-profile .meter-box')) {
		$('.user-profile .meter-box').each(function () {
			var usrmsg = $(this).children('.inner-link');
			var usrmsgtxt = $(this).children('.inner-link').text();
			var usrmsgimg = $(this).children('.prozenten');
			if (usrmsgtxt !== 'Profiil 100% valmis') {
				usrmsg.css('visibility', 'visible');
				usrmsgimg.css('visibility', 'visible');
			}
		});
	}
}
/* checkbox dependency */
function eduToggle() {
	$('#ctl00_PlaceHolderMain_interestsList_3').click(function () {
		$('#enter-education').toggle();
		$('#enter-education').animate({ opacity: 0 }, 0)
			.animate({ opacity: 1 }, 400);
	});
}
/* clear txt inputs */
function clearInputs() {
	$('input.clear').each(function () {
		this.val = this.value;
		this.onfocus = function () {
			if (this.value == this.val) {
				this.value = '';
				$(this).removeClass('clear');
			}
		}
		this.onblur = function () {
			if (this.value == '') {
				this.value = this.val;
				$(this).addClass('clear');
			}
		}
	});
}
/* IE6 hover */
function ieHover(h_list) {
    if (!$.browser)
        return;
    if (!$.browser.msie)
        return;

	if ($.browser.msie && $.browser.version < 7) {
		$(h_list).hover(function () {
			$(this).addClass('hover');
			if (this.getElementsByTagName("ul")[0] && typeof (hideSelectBoxes) == 'function') {
				hideSelectBoxes(this.getElementsByTagName("ul")[0]);
			}
		}, function () {
			$(this).removeClass('hover');
			if (this.getElementsByTagName("ul")[0] && typeof (hideSelectBoxes) == 'function') {
				showSelectBoxes(this.getElementsByTagName("ul")[0]);
			}
		});
	}
	if ($.browser.msie && $.browser.version < 7) {
		$(h_list).hover(function () {
			$(this).addClass('hover');
			if (this.getElementsByTagName("div")[0] && typeof (hideSelectBoxes) == 'function') {
				hideSelectBoxes(this.getElementsByTagName("div")[0]);
			}
		}, function () {
			$(this).removeClass('hover');
			if (this.getElementsByTagName("div")[0] && typeof (hideSelectBoxes) == 'function') {
				showSelectBoxes(this.getElementsByTagName("ul")[0]);
			}
		});
	}
}
/* slide */
function initSlide() {
	if ($('div#block').length) {
		$('a.toggle').click(function () {
			$('div#block').slideToggle();
		});
	}
}
/* close block */
function initClose() {
	$('a.close').click(function () {
		$(this).parent().parent().parent().toggleClass('none-box');
		return false;
	});
	$('a.close2').click(function () {
		$(this).parent().toggleClass('none-box');
		return false;
	});
}
/* slide gallery */
function initGall() {
	$('.gallery').each(function () {
		var btn_prev = $(this).children('.link-prev').addClass('disabled');
		var btn_next = $(this).children('.link-next').addClass('disabled');
		var list_h = $(this).find('div > ul');
		var _list = list_h.children();
		var _step = list_h.parent().width();
		var _w = 0;
		var _m = 0;
		_list.each(function () {
			_w += this.offsetWidth;
		});
		if (_w > _step) {
			btn_next.removeClass('disabled');
			btn_prev.click(function () {
				if (!$(this).hasClass('disabled')) changeEl(false);
				return false;
			});
			btn_next.click(function () {
				if (!$(this).hasClass('disabled')) changeEl(true);
				return false;
			});
		}
		function changeEl(_f) {
			if (_f) {
				if (_m + _step < _w - _step) _m += _step;
				else _m = _w - _step;
			}
			else {
				if (_m - _step > 0) _m -= _step;
				else _m = 0;
			}
			if (_m == _w - _step) btn_next.addClass('disabled');
			else btn_next.removeClass('disabled');
			if (_m == 0) btn_prev.addClass('disabled');
			else btn_prev.removeClass('disabled');
			list_h.animate({ marginLeft: -_m }, 1000, function () {
				_flag = true;
			});
		}
	});
}
/* accordion */
function initAccordion() {
	var _hold = $('.members-main .accordion');
	if (_hold.length) {
		var _list = _hold.children();
		var _a = _list.index(_list.filter('.active'));
		if (_a == -1) _a = 0;
		_list.removeClass('active').eq(_a).addClass('active');
		_list.each(function (_i) {
			this._btn = $(this).find('a.opener');
			this._w = this._btn.parent().outerWidth();
			if (_i == _a) {
				this._btn.find('img').css('opacity', 1);
				$(this).width(361);
			}
			else {
				this._btn.find('img').css('opacity', 0.7);
				$(this).width(this._w);
			}
			this._btn.click(function () {
				changeEl(_i);
				return false;
			});
		});
		function changeEl(_ind) {
			if (_ind != _a) {
				_list.eq(_a).removeClass('active');
				_list.eq(_ind).addClass('active');
				_list.eq(_a).animate({ width: _list.get(_a)._w }, { queue: false, duration: 500 });
				_list.eq(_ind).animate({ width: 361 }, { queue: false, duration: 500 });
				_list.get(_a)._btn.find('img').animate({ opacity: 0.7 }, { queue: false, duration: 500 });
				_list.get(_ind)._btn.find('img').animate({ opacity: 1 }, { queue: false, duration: 500 });
				_a = _ind;
			}
		}
	}
}
/* safari detect */
function initPage() {
	if (navigator.appVersion.indexOf("Safari") != -1) {
		if (!window.getComputedStyle) document.body.className += " safari";
		else document.body.className += " safari3";
	}
}
if (window.addEventListener) window.addEventListener("load", initPage, false);
else if (window.attachEvent && !window.opera) window.attachEvent("onload", initPage);
/* link hover */
function initLink() {
	_link = document.getElementById('user-menu');
	_link2 = document.getElementById('user-menu2');
	if (_link) {
		_link.onmouseover = function () {
			this.style.zIndex = 40;
		}
		_link.onmouseout = function () {
			this.style.zIndex = 2;
		}
	}
	if (_link2) {
		_link2.onmouseover = function () {
			this.style.zIndex = 40;
		}
		_link2.onmouseout = function () {
			this.style.zIndex = 2;
		}
	}
}
if (window.addEventListener) window.addEventListener("load", initLink, false);
else if (window.attachEvent && !window.opera) window.attachEvent("onload", initLink);
/* drop-click.js */
var _id = 'profile-nav';
var _innerEl = 'ul';
var _className = 'active';
var _hide = true;
var _menu;
var _nodes;
function initMenu() {
	_menu = document.getElementById(_id);
	if (_menu) {
		_nodes = _menu.getElementsByTagName('span');
		for (var i = 0; i < _nodes.length; i++) {
			if (_nodes[i].parentNode.getElementsByTagName(_innerEl)[0]) {
				_nodes[i].onclick = function () {
					if (this.parentNode.className.indexOf(_className) != -1) {
						this.parentNode.className = this.parentNode.className.replace(_className, "");
					} else {
						if (_hide) hideLevels(this);
						this.parentNode.className += ' ' + _className;
					}
					return false;
				}
			}
		}
	}
}
function hideLevels(_this) {
	var _lis = _menu.getElementsByTagName('li');
	for (var i = 0; i < _lis.length; i++) {
		var _f = false;
		var _a = _lis[i].getElementsByTagName('a');
		for (var j = 0; j < _a.length; j++) {
			if (_a[j] == _this) _f = true;
		}
		if (!_f) _lis[i].className = _lis[i].className.replace(_className, "");
	}
}
if (window.addEventListener) {
	window.addEventListener("load", initMenu, false);
}
else if (window.attachEvent) {
	window.attachEvent("onload", initMenu);
}
/* hide-select.js */
function hideSelectBoxes(object) {
	if (!object) return;
	if (!object.sboxes)
		object.sboxes = [];
	var ol = getElementX(object);
	var ot = getElementY(object);
	var ow = object.offsetWidth;
	var oh = object.offsetHeight;
	var sboxes = document.all.tags("select");
	for (var i = 0; i < sboxes.length; i++) {
		var node = sboxes[i].parentNode;
		while (node != object && node.tagName != "BODY")
			node = node.parentNode;
		var skip = (node == object);
		if (skip) continue;
		var t = getElementY(sboxes[i]);
		var l = getElementX(sboxes[i]);
		var w = sboxes[i].offsetWidth;
		var h = sboxes[i].offsetHeight;
		var ver = false;
		if (t > ot && t < (ot + oh))
			ver = true;
		else if ((t + h) > ot && (t + h) < (ot + oh))
			ver = true;
		var hor = false;
		if (l > ol && l < (ol + ow))
			hor = true;
		else if ((l + w) > ol && (l + w) < (ol + ow))
			hor = true;
		else if (l < ol && (l + w) > ol)
			hor = true;
		if (ver && hor && sboxes[i].style.visibility != "hidden")
			object.sboxes[object.sboxes.length] = sboxes[i];
	}
	for (var i = 0; i < object.sboxes.length; i++)
		object.sboxes[i].style.visibility = "hidden";
}
function showSelectBoxes(object) {
	if (!object) return;
	if (!object.sboxes) return;
	for (var i = 0; i < object.sboxes.length; i++)
		object.sboxes[i].style.visibility = "";
	object.sboxes = [];
}
function getElementX(object) { return getElementC(object, true) }
function getElementY(object) { return getElementC(object, false) }
function getElementC(element, xAxis) {
	var initialElement = element;
	var c = 0;
	while (element != null) {
		c += (xAxis) ? element.offsetLeft : element.offsetTop;
		if (element.style.position == "absolute")
			break;
		else
			element = element.offsetParent;
	}
	var elementWnd = document.window;
	if (!elementWnd) return c;
	if (!elementWnd.frameElement) return c;
	return c + getElementC(elementWnd.frameElement, xAxis);
}
/* input-type-file.js */
function initCustomFile() {
	var _inputs = document.getElementsByTagName("input");
	for (var i = 0; i < _inputs.length; i++) {
		if (_inputs[i].className.indexOf("file-input-area") != -1) {
			var _file_input = _inputs[i];
			if (window.attachEvent) {
				_file_input.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=0)'; // IE7, IE8
			}
			_file_input._fake_file_input = _inputs[i].parentNode.getElementsByTagName('input')[1];
			_file_input._fake_file_input.readOnly = true;
			_file_input.onchange = function () {
				this._fake_file_input.value = this.value;
			}
		}
	}
}
if (window.addEventListener)
	window.addEventListener("load", initCustomFile, false);
else if (window.attachEvent)
	window.attachEvent("onload", initCustomFile);